package com.otomotion.research.ants.client;

import gnu.cajo.invoke.Remote;

import java.rmi.NotBoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.otomotion.research.ants.information.Coordinate;

/**
 * Created by IntelliJ IDEA. User: Mark the intern Date: 1-Aug-2007 Time:
 * 2:58:21 PM
 */

public class Client extends Thread {

	String hostname;
	Integer port;
	Object remoteServer;
	Boolean connected = false;
	DisplaySystem display;
	Node n;
	int aID = 0;
	int size = 2;

	public Client(String hostname, int port, Node n, DisplaySystem display) {

		this.n = n;
		this.display = display;
		this.hostname = hostname;
		this.port = port;
		while (!connected) {
			try {
				System.out.println("Connecting to server at host " + hostname
						+ " on port " + port);
				remoteServer = Remote.getItem("//" + hostname + ":" + port
						+ "/aServer");
				connected = true;
				System.out.println("Connected!");
			} catch (Exception e) {
				System.out
						.println("Could not connect to server.  Will reconnect in 5 seconds");
				e.printStackTrace();
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {

			}
		}
	}

	public void run() {

		boolean quit = false;
		while (!quit) {
			try {
				Object[] args = new Object[2];
				args[0] = new Integer(aID);
				args[1] =new Integer(size);
				Document d = (Document) Remote.invoke(remoteServer,
						"getInformation", args);
				System.out.println(new XMLOutputter().outputString(d));

				Node t = this.buildNode(d);
				n.detachAllChildren();
				n.attachChild(t);
				// System.out.println("Value is " + d.toString());
			} catch (Exception e) {

				e.printStackTrace();
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				System.out.println("Client process interrupted.  Quitting.");
				quit = true;
			}
		}
	}

	private Node buildNode(Document d) {

		Node n = new Node("data");
		if (d == null)
			return null;
		Element root = d.getRootElement();
		List children = root.getChildren();
		Iterator i = children.iterator();
		while (i.hasNext()) {
			Element e = (Element) i.next();
			float x = Float.parseFloat(e.getAttributeValue("x"));
			float y = Float.parseFloat(e.getAttributeValue("y"));
			float z = Float.parseFloat(e.getAttributeValue("z"));
			if (e.getChild("dirt")!=null)
			{
				n.attachChild(getBox(x,z,y,true));
			}
			if (e.getChild("ant")!=null)
			{
				n.attachChild(getBox(x,z,y,false));
			}

		}
		
		return n;

	}

	// public static void main(String args[]) throws Exception {
	// String hostname = "MARKCA-1";
	// int port = 5635;
	// Client myClient = new Client(hostname, port);
	// myClient.start();
	// }

	private Box getBox(float x, float y, float z,boolean dirt) {
		Vector3f min = new Vector3f(-1, -1, -1);
		Vector3f max = new Vector3f(1, 1, 1);

		AlphaState as1 = display.getRenderer().createAlphaState();
		as1.setBlendEnabled(true);
		as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);
//		as1.setSrcFunction(AlphaState.DB_ZERO);
		as1.setDstFunction(AlphaState.DB_DST_ALPHA);
		as1.setTestEnabled(true);
		as1.setTestFunction(AlphaState.TF_GREATER);
		as1.setEnabled(true);



		
		
		
		
		Box box;
		box = new Box("box", min.mult(.5f), max.mult(.5f));
		if (dirt)
		{
			box.setDefaultColor(new ColorRGBA().brown);
//			box.setRenderState(as1);
		}
		else
		{
			box.setDefaultColor(new ColorRGBA().red);
		}
			
		box.setModelBound(new BoundingSphere());
		box.updateModelBound();
		box.setLightCombineMode(LightState.OFF);
		box.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
		box.setLocalTranslation(x, y, z);
		box.updateRenderState();
		return box;
	}

	public void nextAnt() {
		aID++;
		
	}

	public void prevAnt() {
		aID--;
		if (aID<0)
			aID=0;
		
	}

	public void increaseSize() {
		size++;
		
	}

	public void decreaseSize() {
		size--;
		if (size<0)
			size=0;
		
	}
}